//创建日志,可使用{SourceContext}记录

using Serilog;
using Serilog.Events;
using UserService.HttpApi.Host;

Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Debug()
    .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
    .MinimumLevel.Override("Microsoft.AspNetCore.Hosting.Diagnostics", LogEventLevel.Error)
    .MinimumLevel.Override("Quartz", LogEventLevel.Warning)
    .Enrich.FromLogContext()
    .WriteTo.Async(c => c.File("logs/all/log-.txt", rollingInterval: RollingInterval.Day,
        restrictedToMinimumLevel: LogEventLevel.Debug))
    .WriteTo.Async(c => c.File("logs/error/errorlog-.txt", rollingInterval: RollingInterval.Day,
        restrictedToMinimumLevel: LogEventLevel.Error))
    .WriteTo.Async(c => c.Console())
    .CreateLogger();


try
{
    Log.Information(@"
          ___   __  __   ____                  _                      
         |_ _| |  \/  | / ___|   _   _   ___  | |_    ___   _ __ ___  
          | |  | |\/| | \___ \  | | | | / __| | __|  / _ \ | '_ ` _ \ 
          | |  | |  | |  ___) | | |_| | \__ \ | |_  |  __/ | | | | | |
         |___| |_|  |_| |____/   \__, | |___/  \__|  \___| |_| |_| |_|
                                 |___/                                                      
    ");
    
    Log.Information("IMSystem，启动！");
    var builder = WebApplication.CreateBuilder(args);
    Log.Information($"当前主机启动环境-【{builder.Environment.EnvironmentName}】");
    Log.Information($"当前主机启动地址-【{builder.Configuration["App:SelfUrl"]}】");
    
    builder.WebHost.UseUrls(builder.Configuration["App:SelfUrl"]);
    builder.Host.UseAutofac();
    builder.Host.UseSerilog();
    await builder.Services.AddApplicationAsync<UserServiceHttpApiHostModule>();
    var app = builder.Build();
    await app.InitializeApplicationAsync();
    await app.RunAsync();
}
catch (Exception ex)
{
    Log.Fatal(ex, "IMSystem，爆炸！");
}
finally
{
    Log.CloseAndFlush();
}
